package com.zemb.flxwear.Utils;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.pdf.PdfDocument;

import com.zemb.flxwear.Bean.ReportItem;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class PDFUtils {
    Context context;
    public PDFUtils(Context context) {
        this.context = context;
    }
    public void createPDFInThread(String path, String fileName, List<ReportItem> reportItems,String phone){
        new Thread(new Runnable() {
            @Override
            public void run() {
                createPDF(path, fileName,reportItems,phone);
            }
        }).start();
    }

    private void createPDF(String path, String fileName ,List<ReportItem> reportItems,String phone){
        int y= 64;
        File file = new File(path, fileName);
        //输出file路径
        LogUtils.PrintLog("file:"+ file.getAbsolutePath());

        PdfDocument document = new PdfDocument();

// 创建一个页面并设置页面尺寸和边距 A4纸张大小

          PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(595, 842, 1).create();
        PdfDocument.Page page = document.startPage(pageInfo);

// 获取 Canvas 对象
        Canvas canvas = page.getCanvas();

// 设置画笔颜色和字体大小
        Paint paint = new Paint();
        paint.setColor(Color.BLACK);
        //三号字体大小
        paint.setTextSize(32);
        //横向居中写“体检报告”
        canvas.drawText("体检报告", 595/2-32*3, y, paint);

        //四号字体大小
        paint.setTextSize(16);
        //
        y+=32;
        //居中写“用户：”+手机号
        canvas.drawText("用户："+phone, 595/2-24*3, y, paint);

        for (ReportItem reportItem:reportItems){
            y+=32;
            switch (reportItem.type){
                case BLOOD_SUGAR:
                    paint.setColor(Color.parseColor("#4CAF50"));
                    canvas.drawText("血压：", 32, y, paint);

                    break;
                case BLOOD_PRESSURE:
                    paint.setColor(Color.parseColor("#FF9800"));
                    canvas.drawText("血糖：", 32, y, paint);
                    break;
                case BLOOD_OXYGEN:
                    paint.setColor(Color.parseColor("#2196F3"));
                    canvas.drawText("血氧：", 32, y, paint);
                    break;
                case ECG:
                    paint.setColor(Color.parseColor("#9C27B0"));
                    canvas.drawText("心电：", 32, y, paint);
                    break;
                case URIC_ACID:
                    paint.setColor(Color.parseColor("#F44336"));
                    canvas.drawText("尿酸：", 32, y, paint);
                    break;
                case CHOLESTEROL:
                    paint.setColor(Color.parseColor("#FFEB3B"));
                    canvas.drawText("胆固醇：", 32, y, paint);
                    break;
                case BLOOD_KETONE:
                    paint.setColor(Color.parseColor("#00BCD4"));
                    canvas.drawText("血酮：", 32, y, paint);
                    break;
            }

            canvas.drawText(reportItem.result, 100, y, paint);
            //居中 写“时间：”+时间
            canvas.drawText(reportItem.time, 595/2, y, paint);


        }

// 在 Canvas 上绘制中文文本

// 结束页面并保存文档
        document.finishPage(page);
        try {
            if(file.createNewFile()){
                System.out.println("create file success");
            }else {
                System.out.println("create file fail");
                return;
            }
            FileOutputStream fos = new FileOutputStream(file);
            document.writeTo(fos);
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        document.close();
    }

}
